package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.constant.SettleType;
import com.cloudfun.campusshare.common.constant.ShopType;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.KjtSubMerchantDTO;
import com.cloudfun.campusshare.common.model.dto.WithdrawApplicationDTO;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtSubMerchantChangeSettleTypeDTO;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtSubMerchantCreateDTO;
import com.cloudfun.campusshare.common.model.dto.kjt.KjtSubMerchantResubmissionDTO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.KjtSettleTypeChgApplicationService;
import com.cloudfun.campusshare.service.KjtSubMerchantService;
import com.cloudfun.campusshare.service.WithdrawApplicationService;
import com.cloudfun.campusshare.util.AmountUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;

/**
 * Created by Huqin on 2020/8/10
 */
@RestController
@RequestMapping("/v1/kjt")
@Api(value = "快捷通")
@Validated
@Slf4j
public class KjtSubMerchantController {

    @Autowired
    private KjtSubMerchantService subMerchantService;

    @Autowired
    private KjtSettleTypeChgApplicationService settleTypeChgApplicationService;

    @Autowired
    private WithdrawApplicationService withdrawApplicationService;


    @ApiOperation(value = "跳转快捷通页面创建子商户", notes = "跳转快捷通页面创建子商户")
    @RequestMapping(value = "/createSubMerchantPage/{userId}", method = RequestMethod.GET)
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<String> createSubMerchantPage(@PathVariable String userId) {

        return BaseResultFactory.produceSuccess(subMerchantService.createSubMerchantPage(userId));
    }

    @ApiOperation(value = "创建子商户", notes = "创建子商户")
    @RequestMapping(value = "/createSubMerchant", method = RequestMethod.POST)
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> createSubMerchant(@Valid @RequestBody KjtSubMerchantCreateDTO createDTO) {
        // 店面类型
        if (ShopType.ONLINE_SHOP == createDTO.getShopType()) {
            if (StringUtils.isBlank(createDTO.getNetworkUrlPhotoId())) {
                throw new BusinessException(Code.PARAMETER_ERROR, "店面类型为网店时，网店完整的域名截图照片不能为空");
            }
        } else {
            if (CollectionUtils.isEmpty(createDTO.getStoreIds())) {
                throw new BusinessException(Code.PARAMETER_ERROR, "店面类型为门店时，商户经营场所照片不能为空");
            }
        }
        // 委托提现
        if (SettleType.ENTRUST_WITHDRAWAL == createDTO.getSettleType()) {
            if (StringUtils.isBlank(createDTO.getEntrustWithdrawalAmount())) {
                throw new BusinessException(Code.PARAMETER_ERROR, "结算方式为委托提现时，委托提现起结金额不能为空");
            }
            if (AmountUtil.strAmount2Int(createDTO.getEntrustWithdrawalAmount()) <= 0) {
                throw new BusinessException(Code.PARAMETER_ERROR, "委托提现起结金额错误");
            }
        }
        // 三证合一
        if (!createDTO.getThreeCertFlag()) {
            if (StringUtils.isBlank(createDTO.getOrganizationNumberId()) || StringUtils.isBlank(createDTO.getOrganizationCode())
                    || StringUtils.isBlank(createDTO.getTaxId()) || StringUtils.isBlank(createDTO.getTaxRegistryNo())) {
                throw new BusinessException(Code.PARAMETER_ERROR, "三证合一时，组织机构代码证号、税务登记证号及其影印件不能为空");
            }
        }
        // 设置营业执照有效期
        if (null == createDTO.getBusinessLicenseValidity()) {
            createDTO.setBusinessLicenseValidity(LocalDate.of(2999, 1, 1));
        }
        // 设置身份证有效期
        if (null == createDTO.getCertificatesValidity()) {
            createDTO.setCertificatesValidity(LocalDate.of(2999, 1, 1));
        }
        subMerchantService.createSubMerchant(createDTO);
        return BaseResultFactory.produceSuccess();
    }


    @ApiOperation(value = "准入资料重提", notes = "准入资料重提")
    @RequestMapping(value = "/resubmission", method = RequestMethod.POST)
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> resubmission(@Valid @RequestBody KjtSubMerchantResubmissionDTO resubmissionDTO) {
        // 店面类型
        if (ShopType.ONLINE_SHOP == resubmissionDTO.getShopType()) {
            if (StringUtils.isBlank(resubmissionDTO.getNetworkUrlPhotoId())) {
                throw new BusinessException(Code.PARAMETER_ERROR, "店面类型为网店时，网店完整的域名截图照片不能为空");
            }
        } else {
            if (CollectionUtils.isEmpty(resubmissionDTO.getStoreIds())) {
                throw new BusinessException(Code.PARAMETER_ERROR, "店面类型为门店时，商户经营场所照片不能为空");
            }
        }
        // 三证合一
        if (!resubmissionDTO.getThreeCertFlag()) {
            if (StringUtils.isBlank(resubmissionDTO.getOrganizationNumberId()) || StringUtils.isBlank(resubmissionDTO.getOrganizationCode())
                    || StringUtils.isBlank(resubmissionDTO.getTaxId()) || StringUtils.isBlank(resubmissionDTO.getTaxRegistryNo())) {
                throw new BusinessException(Code.PARAMETER_ERROR, "三证合一时，组织机构代码证号、税务登记证号及其影印件不能为空");
            }
        }
        // 设置营业执照有效期
        if (null == resubmissionDTO.getBusinessLicenseValidity()) {
            resubmissionDTO.setBusinessLicenseValidity(LocalDate.of(2999, 1, 1));
        }
        // 设置身份证有效期
        if (null == resubmissionDTO.getCertificatesValidity()) {
            resubmissionDTO.setCertificatesValidity(LocalDate.of(2999, 1, 1));
        }
        subMerchantService.resubmission(resubmissionDTO);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "注册查询", notes = "注册查询")
    @RequestMapping(value = "/regQuery", method = RequestMethod.GET)
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> regQuery() {
        KjtSubMerchantDTO res = subMerchantService.regQuery();
        return BaseResultFactory.produceSuccess(res);
    }

    @ApiOperation(value = "申请修改结算方式", notes = "申请修改结算方式")
    @RequestMapping(value = "/chgSettleType", method = RequestMethod.PUT)
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> chgSettleType(@Valid @RequestBody KjtSubMerchantChangeSettleTypeDTO changeSettleTypeDTO) {
        settleTypeChgApplicationService.changeSettleType(changeSettleTypeDTO);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "提现", notes = "提现")
    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> withdraw(@Valid @RequestBody WithdrawApplicationDTO withdrawApplicationDTO) {
        withdrawApplicationService.withdraw(withdrawApplicationDTO);
        return BaseResultFactory.produceSuccess();
    }
}
